package UDP.Multicast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ReceiveMessageDemo1 {
    public static void main(String[] args) throws IOException {
        /**
         * 组播接收端
         */
        // 1. 创建MulticastSocket对象
        MulticastSocket ms = new MulticastSocket(10000);
        // 2. 将当前本机，添加到224.0.0.1 的这一组当中
        InetAddress address = InetAddress.getByName("224.0.0.1");
        ms.joinGroup(address);
        //3、数据接收包
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
        //4. 接收数据
        ms.receive(dp);
        //5. 解析数据
        byte[] data = dp.getData();
        int len = dp.getLength();
        String ip = dp.getAddress().getHostAddress();
        String name = dp.getAddress().getHostName();
        //5.释放资源
        ms.close();
    }
}
